package com.tanhua.sso.service;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.dubbo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyCenterService {
    @Autowired
    private  UserService userService;

    @Autowired
    private  SmsService smsService;

    public Object sendVerificationCode(String token) {
        Long userId = this.userService.checkToken(token);

        //根据用户id查询用户信息
        User user = this.userService.queryUserByUserId(userId);
        if(ObjectUtil.isEmpty(user)){
            return ErrorResult.builder().errCode("401").errMessage("用户不存在").build();
        }
        //用户信息存在，发送验证码，更新数据信息

        return this.smsService.sendCheckCode(user.getMobile());
    }

    public Boolean checkVerificationCode(String token,String code) {
        //根据token,获取用户手机号
        Long userId = this.userService.checkToken(token);
        User user = this.userService.queryUserByUserId(userId);

        Boolean result = this.userService.updateLogin(code, user);
        return result;
    }

    public Object updatePhone(String token, String phone) {
        Long userId = this.userService.checkToken(token);
        return this.userService.updatePhone(userId, phone);
    }
}
